﻿using Decoration.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Decoration.Repository
{
    public class BaseRepository<T> :BaseQueryRepository,IBaseRepository<T> where T : class, new()
    {
        protected DbContext ctx;
        public BaseRepository(DbContext ctx)
        {
            this.ctx = ctx;
        }
        public void Add(T t)
        {
            ctx.Set<T>().Add(t);
        }

        public void Delete(T t)
        {
            ctx.Set<T>().Remove(t);
        }

        public T Get(Expression<Func<T, bool>> whereLambda)
        {
            return ctx.Set<T>().Where(whereLambda).FirstOrDefault();
        }

        public IQueryable<T> Query(Expression<Func<T, bool>> whereLambda)
        {
            return ctx.Set<T>().Where(whereLambda);
        }

        public void Update(T t)
        {
            ctx.Set<T>().Update(t);
        }

        //保存
        public bool SaveChanges()
        {
            var res = ctx.SaveChanges();
            return res > 0;
        }
    }
}
